package com.example.addressbook;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.content.Intent; 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.RecyclerView;
import android.content.Context;
import com.bumptech.glide.Glide;
import org.litepal.crud.DataSupport;
import android.widget.PopupMenu;
import android.app.Dialog;
import android.view.MenuItem;
import android.view.Gravity; 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.ContextMenu;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v4.content.ContextCompat;
import android.content.pm.PackageManager; 
import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.app.AlertDialog;
import android.os.Build; 
import android.support.v4.content.FileProvider;  
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.design.widget.Snackbar;   
import android.graphics.Color;  
  
import java.util.*;
import java.io.*;

public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder>{
	
	private Context context;
	private List<Friend> friends = new ArrayList<>();

	public FriendAdapter(Context c){
		context = c;		// getActivity()
		getDataFromDB();
	}
	static class ViewHolder extends RecyclerView.ViewHolder{
		ImageView imageView;
		TextView textView;
		ImageView menu;
		
		public ViewHolder(View v){
			super(v);
			imageView = (ImageView)v.findViewById(R.id.circle_image_view);
			textView = (TextView)v.findViewById(R.id.text_view);
			menu = (ImageView)v.findViewById(R.id.menu_image_view);
		}
	}
	
	private Context mContext;
	
	//private static boolean isDelete = true;
	
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
		mContext = parent.getContext();		
		View view = LayoutInflater.from(parent.getContext())
			.inflate(R.layout.recycler_view_2_item,parent,false);
			final ViewHolder holder = new ViewHolder(view);
			
			holder.imageView.setOnClickListener( new View.OnClickListener(){
				@Override
				public void onClick(View v){
					final int position = holder.getAdapterPosition();
					
					final Friend ff = friends.get(position);
					Snackbar.make(v,"已从列表中移除该项",Snackbar.LENGTH_SHORT)
						.setAction("取消",new View.OnClickListener(){
							@Override
							public void onClick(View v){
								Toast.makeText(v.getContext(),"该项已恢复",Toast.LENGTH_SHORT).show();
		//						isDelete = false;
								Friend newF = new Friend(ff.getName(),ff.getTelephone(),
							ff.getInformation(),ff.getPicturePath());
								newF.save();
								friends.add(position,newF); //
								notifyItemInserted(position);
								notifyItemRangeChanged(position,friends.size()-position);
							}
						})
						.show();
					delete(position);
					//Toast.makeText(v.getContext(),"you click view:"+position,Toast.LENGTH_SHORT).show();
						
				}
			});
			view.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View v){
					int position = holder.getAdapterPosition();
					String telNumber = friends.get(position).getTelephone();
					Intent intent = new Intent(Intent.ACTION_CALL);
					intent.setData(Uri.parse("tel:"+telNumber));
					((MainActivity)context).startActivityForResult(intent,3);
				}
			});
			holder.menu.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View v){
					int position = holder.getAdapterPosition();		
					Friend f = friends.get(position);
					Intent intent = new Intent(context,ItemDetailActivity.class);	//go to detail
					intent.putExtra("tag","friend");
					intent.putExtra("friend_data",f);
					((MainActivity)context).startActivityForResult(intent,2);
				}
			});
			holder.menu.setImageResource(R.drawable.ic_exit_to_app_grey600_24dp);
			holder.menu.setColorFilter(Color.parseColor("#0099CC"));
			return holder;
	}
	
	@Override
	public void onBindViewHolder(ViewHolder holder,int position){
		Friend f = friends.get(position);
		Glide.with(mContext).load(f.getPicturePath()).into(holder.imageView);
		holder.textView.setText(f.getName());
	}
	
	@Override
	public int getItemCount(){
		return friends.size();
	}
	
	public void getDataFromDB(){
		friends = DataSupport.findAll(Friend.class);	//database
	}
	
	public void delete(int position){
		DataSupport.deleteAll(Friend.class,"name = ?",friends.get(position).getName());		//database
		friends.remove(position);
		notifyItemRemoved(position);
		notifyItemRangeChanged(position,friends.size()-position);
	}
}